package wxPaySdk.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.cfg.WxCfg;
import wxPaySdk.IWXPayDomain;
import wxPaySdk.WXPayConfig;
import wxPaySdk.WXPayConstants;

import java.io.*;

/**
 * Created by asus on 2018/10/8.
 */
@Service
public class WXPayConfigImpl extends WXPayConfig {
    @Autowired
    WxCfg wxCfg;

    /**
     * 获取 App ID
     *
     * @return App ID
     */
    @Override
    public String getAppID() {
        return wxCfg.getApp_id();
    }

    /**
     * 获取 Mch ID
     *
     * @return Mch ID
     */
    @Override
    public String getMchID() {
        return wxCfg.getMch_id();
    }

    /**
     * 获取 API 密钥
     *
     * @return API密钥
     */
    @Override
    public String getKey() {
        return wxCfg.getApi_key();
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return super.getHttpConnectTimeoutMs();
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return super.getHttpReadTimeoutMs();
    }

    @Override
    public IWXPayDomain getWXPayDomain() {
        IWXPayDomain iwxPayDomain = new IWXPayDomain() {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex) {
                domain = WXPayConstants.DOMAIN_API;
            }

            @Override
            public DomainInfo getDomain(WXPayConfig config) {
                DomainInfo domainInfo = new DomainInfo(WXPayConstants.DOMAIN_API, true);
                return domainInfo;
            }
        };
        return iwxPayDomain;
    }

    @Override
    public boolean shouldAutoReport() {
        return super.shouldAutoReport();
    }

    @Override
    public int getReportWorkerNum() {
        return super.getReportWorkerNum();
    }

    @Override
    public int getReportQueueMaxSize() {
        return super.getReportQueueMaxSize();
    }

    @Override
    public int getReportBatchSize() {
        return super.getReportBatchSize();
    }

    /**
     * 获取商户证书内容
     *
     * @return 商户证书内容
     */
    @Override
    public InputStream getCertStream() throws Exception {
        FileInputStream instream = new FileInputStream(new File(wxCfg.getCert_path()));
        return instream;
    }
}
